package cn.biplam.front.template.base;

import java.util.Map;

import cn.biplam.common.tools.TOOLS;
import cn.biplam.common.wrapper.ErrorMessage;
import cn.biplam.common.wrapper.KvMap;
import cn.biplam.front.html.HtmlDiv;
import cn.biplam.front.html.HtmlValue;
import cn.biplam.front.html.base.HtmlWindow;
import cn.biplam.front.html.base.Const.Position_Mode;
import cn.biplam.front.html.base.Const.Visibility_Mode;

/**
 * 用于页面隐藏参数的Div <br>
 * 使用方法：
 * <UL>
 * 		<LI>初始化类       HiddenParams param new HiddenParams(ID)
 * 		<LI>设置外部隐藏参数 param.set(paramName,paramValue)
 * 		<LI>取页面对象      param.get();
 * </UL>
 * @author WangSong 2009-6-25下午05:00:52
 * @version 1.0
 */
public class HiddenParams {
	
	private String ID;
	private KvMap<String,String> alParam;
	private String rid;
	
	public static final String hiddenId="h";
	
	public HiddenParams(){
		this("");
	}
	
	/**
	 * 构造函数
	 * @param ID 标识ID
	 */
	public HiddenParams(String ID){
		this.ID = hiddenId+ID;
		this.alParam=new KvMap<String, String>();
	}
	
	/**
	 * 构造函数
	 *  <li>传入rid后,生成rid隐藏表单、rid不能为null
	 * @param ID 标识ID
	 * @param rid 业务对象主键
	 * @throws ErrorMessage
	 */
	public HiddenParams(String ID,String rid) throws ErrorMessage{
		TOOLS.checkNull(rid, "HiddenParams(String ID,String rid) error,rid");
		this.ID = hiddenId+ID;
		this.rid = rid;
		this.alParam=new KvMap<String, String>();
	}
	
	/**
	 * 设置隐藏表单
	 * @param name   参数名称,此参数名不需要用","分隔 ,不能为null
	 * @param value  参数值  可为null
	 * @throws ErrorMessage 
	 */
	public void put(String name,String value) throws ErrorMessage{
		if(name==null || name.trim().equals("")){
			throw new ErrorMessage("HiddenParams.java, name or value is null:"+name+","+value);
		}
		this.alParam.put(name, value);
	}
	
	/**
	 * 返回当前隐藏参数容器ID(一般是h+传入的ID)
	 * @return String
	 */
	public String getID(){
		return this.ID;
	}
	
	/**
	 * 返回按照ID编码规则生成的隐藏参数容器ID
	 * @param id	标识ID(创建隐藏参数容器时的初始化参数)
	 * @return String
	 */
	public static String getIdById(String id){
		return hiddenId+id;
	}
	
	public HtmlWindow get() throws ErrorMessage{
		
		HtmlWindow paramDiv = new HtmlDiv(this.ID);
		/**设置rid*/
		if(rid!=null){
			paramDiv.put(new HtmlValue(ID+"r",rid));
		}
		/** 设置隐藏 */
		paramDiv.setVisibility(Visibility_Mode.Hidden);
		paramDiv.setPositionMode(Position_Mode.Absolute);
		/** 设置参数*/
		for(Map.Entry<String,String> et:this.alParam){
			paramDiv.put(new HiddenParam(et.getKey(),et.getValue()).get());
		}
		
		return paramDiv;
	}
}
